using System;
using System.Collections;
using Scenario.WorkflowEngine.DataModel;

namespace Scenario.WorkflowEngine.DataModel.WorkflowParticipantsData
{
	//*********************************************************************
	//
	// XMLRoles Class
	//
	// The XMLRoles class represents a collection base of XMLRole
	// Its imports methods from WAPI.XmlObjectSet like get set and add
	// XMLRoles Has no fields
	//
	//*********************************************************************
	public class XMLRoles : bOS.XML.Data.XMLObjectSet
	{
		public const string MAIN_NODE	= "Roles";
		
		/** FIELDS **/

		/** no fields **/

		/** CONSTRUCTORS **/

		/** CONSTRUCTOR Empty **/
		public XMLRoles()
		{
			this.Init();
		}

		/** CONSTRUCTOR Width xml parsering**/
		public XMLRoles(System.Xml.XmlNode nodeList)
		{
			this.Parse (nodeList);
		}

		/** INSTANCE METHODS **/

		override public void Init()
		{
			this.RemoveAll();
		}

		/**Method to parse an xml string into a nodelist collection **/
		override public void Parse(System.Xml.XmlNode nodeList)
		{
			this.Init();

			System.Xml.XmlNode node;

			for ( int i=0; i<nodeList.ChildNodes.Count;i++)
			{
				node = nodeList.ChildNodes[i];

				switch ( node.Name) 
				{
					case XMLRole.MAIN_NODE:
						this.Add ( new XMLRole(node));
						break;
				}
			}
		}

		/**Method to retive a nodelist from collection **/
		override public bOS.XML.XMLNode GetXMLNode()
		{
			bOS.XML.XMLNode xmlNode = new bOS.XML.XMLNode(XMLRoles.MAIN_NODE);

			for ( int i=0; i<this.Count;i++)
			{
				xmlNode.AddNode ( this[i].GetXMLNode() );
			}

			return xmlNode;
		}

		/** method to retive a single role from name **/
		public XMLRole GetRole(string sKey)
		{
			for ( int i=0; i<this.Count;i++)
			{
				XMLRole role = (XMLRole) this.List[i];

				if ( role.Key == sKey )
					return role;
			}

			throw new Exception.RoleNotFound("Role not found");
		}
		
		/** method to remove a single role from name **/
		public void RemoveRole (string sKey)
		{
			for ( int i=0; i<this.Count;i++)
			{
				XMLRole role = (XMLRole) this.List[i];

				if ( role.Key == sKey )
				{
					this.RemoveAt ( i);
					return;
				}
			}

			throw new Exception.RoleNotFound("Role not found");
		}

		/** SORTING  FIELDS **/

		public enum SortFields 
		{
			Key,
			Name,
			Description
		}

		/** SORTING METHODS **/


		public void Sort(SortFields sortField, bool isAscending) 
		{
			switch (sortField) 
			{
				case SortFields.Key:
					InnerList.Sort(new KeyComparer());
					break;
				case SortFields.Name:
					InnerList.Sort(new NameComparer());
					break;
				case SortFields.Description:
					InnerList.Sort(new DescriptionComparer());
					break;
				
			}
			if (!isAscending) InnerList.Reverse();
		}


		private sealed class KeyComparer : IComparer 
		{
			public int Compare(object x, object y) 
			{
				XMLRole first = (XMLRole) x;
				XMLRole second = (XMLRole) y;
				return first.Key.CompareTo(second.Key);
			}
		}

		private sealed class NameComparer : IComparer 
		{
			public int Compare(object x, object y) 
			{
				XMLRole first = (XMLRole) x;
				XMLRole second = (XMLRole) y;
				return first.Name.CompareTo(second.Name);
			}
		}

		private sealed class DescriptionComparer : IComparer 
		{
			public int Compare(object x, object y) 
			{
				XMLRole first = (XMLRole) x;
				XMLRole second = (XMLRole) y;
				return first.Description.CompareTo(second.Description);
			}
		}
	}
}